package cn.xclink.common.mapper.help;

import java.util.Set;

import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.mapperhelper.EntityHelper;

public class SqlHelper extends tk.mybatis.mapper.mapperhelper.SqlHelper {

	/**
	 * where所有列的条件，会判断是否!=null
	 *
	 * @param entityClass
	 * @return
	 */
	public static String whereAllIfColumns(Class<?> entityClass, boolean empty) {
		StringBuilder sql = new StringBuilder();
		sql.append("<where>");
		// 获取全部列
		Set<EntityColumn> columnList = EntityHelper.getColumns(entityClass);
		// 当某个列有主键策略时，不需要考虑他的属性是否为空，因为如果为空，一定会根据主键策略给他生成一个值
		for (EntityColumn column : columnList) {
			String likeSql = "";
			if (column.getJavaType().equals(Long.class)) {
				likeSql = " AND " + column.getColumn() + " =#{" + column.getProperty() + "}";
			} else {
				likeSql = " AND " + column.getColumn() + " LIKE concat('%',#{" + column.getProperty() + "},'%')";
			}
			sql.append(getIfNotNull(column, likeSql, empty));
		}
		sql.append("</where>");
		return sql.toString();
	}

}
